﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace QuestionAndScore
{
    public partial class Form1 : Form
    {
        private int result;
        private int score;
        private int remainTime;
        private int remainQuestion;
        private System.Timers.Timer timer1;
        private System.Timers.Timer timer2;
        public Form1()
        {
            InitializeComponent();
            result = GenerateQuestion();

            score = 0;
            remainTime = 100;
            remainQuestion = 15;

            label4.Text = "剩余时间：" + remainTime + "秒";
            label1.Text = "目前得分：" + score;
            label5.Text = "剩余题量：" + remainQuestion;

            timer1 = new System.Timers.Timer();
            timer1.Interval = 1000;
            timer1.Elapsed += new System.Timers.ElapsedEventHandler((s, e) =>
            {
                remainTime--;
                label4.Text = "剩余时间：" + remainTime + "秒";
                if (remainTime <= 0)
                {
                    // 时间到
                    reset();
                }
            });
            timer1.Enabled = true;

            timer2 = new System.Timers.Timer();
            timer2.Interval = 10000;
            timer2.Elapsed += new System.Timers.ElapsedEventHandler((s, e) =>
            {
                label3.Text = "本题时间到！正确答案为：" + result;
                label3.BackColor = Color.Red;
                result = GenerateQuestion();
                remainQuestion--;
                label5.Text = "剩余题量：" + remainQuestion;
                if (remainQuestion == 0)
                {
                    reset();
                }
            });
            timer2.Enabled = true;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        /**
         * 生成问题，返回答案
         */
        private int GenerateQuestion()
        {
            Random ran = new Random();
            int a = ran.Next(100);
            int b = ran.Next(100);
            int c = ran.Next(100);
            if(b < 50)
            {
                label2.Text = a.ToString() + " + " + c.ToString() + " = ";
                return a + c;
            }
            else
            {
                label2.Text = a.ToString() + " - " + c.ToString() + " = ";
                return a - c;
            }
        }

        // 判断结果是否正确
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) { 
                // 获取输入
                String input = textBox1.Text;
                try
                {
                    int inputNum = int.Parse(input);
                    // 判断
                    if (inputNum != result) {
                        throw new Exception("");
                    }
                    // 输入正确
                    label3.Text = "回答正确！";
                    label3.BackColor = Color.Green;
                    score++;
                    label1.Text = "目前得分：" + score;
                }
                catch (Exception) {
                    // 处理错误输入
                    label3.Text = "回答错误！正确答案为：" + result;
                    label3.BackColor = Color.Red;
                }
                finally
                {
                    // 更新题目
                    textBox1.Text = "";
                    result = GenerateQuestion();
                    remainQuestion--;
                    label5.Text = "剩余题量：" + remainQuestion;
                    if (remainQuestion == 0) {
                        reset();
                    }
                    timer2.Stop();
                    timer2.Start();
                }
            }
        }
        /**
         * 重新开始
         */
        private void reset()
        {
            var msg = MessageBox.Show("答题结束！\n总得分：" + score);
            if(msg == DialogResult.OK)
            {
                score = 0;
                remainTime = 100;
                remainQuestion = 15;

                result = GenerateQuestion();

                label4.Text = "剩余时间：" + remainTime + "秒";
                label1.Text = "目前得分：" + score;
                label5.Text = "剩余题量：" + remainQuestion;

                timer1.Stop();
                timer1.Start();

                timer2.Stop();
                timer2.Start();
            }
        }
    }
}
